<?php

namespace App\Http\Controllers;

//use App\Http\Controllers\Admin\Purchase\Order;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use App\Model;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    use Responser;


    use \App\Plugin\Debuger;


    /**
     * 全局参数配置
     * */
    public static function config()
    {
        $dat = $name = [];

        // 配置属性
        $dat['Common'] = Model\Config::translation();

//        $dat[Model\Purchase\Order::class] = Model\Purchase\Order::translation(null, false);
//        $dat[Model\Purchase\Order::class]['ec'] = \App\Contract\EC::$ec;
//        $dat[Model\Purchase\Order::class]['sku_type'] = Admin\Purchase\Order::$skuType;

        $dat[Model\WMS\Warehouse::class] = Model\WMS\Warehouse::translation(null, false);


       /* $dat['MWS'] = [
            'account' => config('services.mws')
        ];
        $dat['MWS']['account'] = array_combine(
            array_keys($dat['MWS']['account']), collect($dat['MWS']['account'])->pluck('name')->toArray()
        );*/

        // 格式化
        foreach ($dat as $cls=>&$group) {
            $name[] = str_replace('\\', '', str_replace(('App\Model\\'), '', $cls));
            foreach ($group as &$unit) {
                foreach ($unit as $key=>&$val) {
                    $val = [
                        'key' => $key,
                        'val' => $val,
                    ];
                }
                $unit = array_values($unit);
            }
        }

        //不需要key-val 形式的
        $dat['Common']['stripe']['pk']  = 'pk_xxxxxxxxxxxxxxxxx';

        return self::resSuccess('ok', array_combine($name, $dat));

    }


    /**
     * 下载Excel模板
     * @param $dat array
     * @param $filename string
     * */
    static function resExcel (array $dat, $filename)
    {
        return \Excel::download(new \App\Services\Excel\DownloadArray($dat),
            $filename
        );
    }


    static public function wx()
    {
//        return 'wx2';
        $token = 'linbin1470731499';
        if (isset($_GET['echostr'])) {

            //验证成为开发者
            $echoStr = $_GET["echostr"];
            if(self::checkSignature($token)){
                echo $echoStr;
                exit;
            }

        }else{
//            $this->responseMsg();

            \Log::info('无echostr');
            return 'err';
        }
    }

    static function checkSignature($token)
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            \Log::info('OK');
            return true;
        }else{
            return false;
        }
    }

}
